<template>
	<view class="main">
		<view class="flex align between topSearch">
			<view style="width: 83%;">
				<u-search @change="change" placeholder="输入分组名搜索" bgColor="#fff" :showAction="false"
					v-model="merge_keyword"></u-search>
			</view>
			<view class="addBtn flex align" @click="show = true">添加<uni-icons type="plus" size="20"
					color="#fff"></uni-icons></view>
		</view>
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="name flex align between">
				<view>分组名：{{item.name}}</view>
				<view>{{item.addtime}}</view>
			</view>
			<view class="flex align between info">
				<view class="info-info">
					<view class="flex align between info-item">
						<view>绑定账号数</view>
						<view>{{item.user_count}}</view>
					</view>
					<view class="flex align between info-item" v-if="item.poi_address!=''">
						<view>POI地址</view>
						<view>{{item.poi_address}}</view>
					</view>
					<view class="flex align between info-item">
						<view>创建时间</view>
						<view>{{item.addtime}}</view>
					</view>
					<!-- <view class="flex align between info-item">
						<view>绑定POI</view>
						<view>123456789</view>
					</view> -->
				</view>
				<view class="flex align">
					<uni-icons style="margin-right: 20rpx;" type="compose" size="20" color="#D81E06"
						@click="toEditShow(item.id,item.name)"></uni-icons>
					<uni-icons type="trash" size="20" color="#D81E06" @click="toDel(item.id)"></uni-icons>
				</view>

			</view>
			<view class="look" @click="toUrl('/pages/employee/look')" v-if="item.user_count!=0">查看</view>
		</view>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<u-popup :show="show" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">添加</view>
				<view class="title">分组名</view>
				<input placeholder="请输入分组名，建议2-30个字" v-model="keyword" />
				<view class="subBtn" @click="toAdd">确定</view>
			</view>
		</u-popup>
		<u-popup :show="show2" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show2 = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">修改</view>
				<view class="title">分组名</view>
				<input placeholder="请输入分组名，建议2-30个字" v-model="nameE" />
				<view class="subBtn" @click="toEdit">确定修改</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		tuiguangUserGroupIndex,
		tuiguangUserGroupAdd,
		tuiguangUserGroupDel
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				keyword: '',
				show: false,
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				merge_keyword: '',
				idE:'',
				nameE:'',
				show2:false
			};
		},
		onLoad() {
			this.getlist();
			// console.log(decodeURIComponent('%7B%22merge_keyword%22%3A%22LIKE%22%7D'))
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			toEditShow(id, name){
				this.show2 = true
				this.idE = id
				this.nameE = name
			},
			toEdit() {
				if (this.nameE == '') {
					uni.showToast({
						title: '请输入分组名',
						icon: 'none'
					})
					return false
				}
				let obj = {
					'row[name]': this.nameE
				}
				let that = this
				uni.request({
					url: 'https://jzxt.jiugezixun.cn/storeadmin/tuiguang_user_group/edit/ids/' + that.idE,
					data: obj,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'XMLHttpRequest',
						'Cookie': uni.getStorageSync('cookies') // 从存储中读取cookies并设置到请求头
					},
					success: result => {
						if (result.data.code == 1) {
							uni.showToast({
								title: '修改成功'
							})
							that.show2 = false
							setTimeout(() => {
								that.idE = ''
								that.nameE = ''
								that.show = false
								that.page = 0
								that.list = []
								that.getlist()
							}, 500)
						} else {
							uni.showToast({
								title: result.data.msg,
								icon:'none'
							})
						}
					},
					fail: result => {
						uni.hideLoading();
					}
				})
			},
			// 删除
			toDel(id) {
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCom(id)
						} else {

						}
					}

				})
			},
			async delCom(id) {
				let obj = {
					action: 'del',
					ids: id
				}
				const result = await tuiguangUserGroupDel(obj)
				if (result.code == 1) {
					uni.showToast({
						title: '删除成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			},
			async toAdd() {
				if (this.keyword == '') {
					uni.showToast({
						title: '请输入分组名',
						icon: 'none'
					})
					return false
				}
				let obj = {
					'row[name]': this.keyword
				}
				const result = await tuiguangUserGroupAdd(obj)
				if (result.code == 1) {
					uni.showToast({
						title: '添加成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			},
			change() {
				this.page = 0
				this.list = []
				this.getlist()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					merge_keyword
				} = that;

				let obj = {
					menu_url: 'tuiguang_user_group',
					limit: offset,
					offset: page,
					sort: 'id',
					order: 'desc',
					_: new Date().getTime()
					// filter:encodeURIComponent(JSON.stringify(filter)),
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				if (merge_keyword != '') {
					obj.filter = JSON.stringify({
						"name": merge_keyword.toString()
					})
					obj.op = JSON.stringify({
						"name": '='
					})
				}
				that.status = 'loading';
				if (totalPage > page) {
					let result = await tuiguangUserGroupIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			toUrl(u) {
				uni.navigateTo({
					url: u
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		margin: 13rpx 30rpx;

		.topSearch {
			margin-bottom: 14rpx;

			.addBtn {
				background: #FFCDB8;
				color: white;
				border-radius: 14rpx;
				font-size: 20rpx;
				padding: 8rpx 14rpx;
			}
		}

		.list {
			background: white;
			box-shadow: 1rpx 1rpx 5rpx 1rpx gainsboro;
			border-radius: 15rpx;
			margin-bottom: 30rpx;

			.name {
				margin: 0 17rpx;
				padding: 26rpx 15rpx 8rpx;
				border-bottom: solid 1rpx #D6D6D6;

				view:nth-child(1) {
					font-size: 28rpx;
					font-weight: bold;
				}

				view:nth-child(2) {
					font-size: 18rpx;
					color: #868686;
				}
			}

			.info {
				font-size: 24rpx;
				padding: 14rpx 41rpx;

				.info-info {
					width: 75%;

					.info-item {
						margin-bottom: 10rpx;
						view:nth-child(2) {
							width: 70%;
							font-weight: bold;
							text-align: center;
						}
					}
				}
			}

			.look {
				border-radius: 0 0 15rpx 15rpx;
				text-align: center;
				color: white;
				font-weight: bold;
				font-size: 32rpx;
				padding: 12rpx 0;
				background: #2D2D2D;
			}
		}

		.btomShow {
			padding: 0 28rpx 150rpx;
			position: relative;

			.showTit {
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				padding: 33rpx 0;
			}

			input {
				font-size: 20rpx;
				padding: 16rpx 24rpx;
				border-radius: 10rpx;
				border: solid 1rpx #707070;
			}

			.closeempty {
				position: absolute;
				top: 20rpx;
				right: 30rpx;

			}

			.title {
				font-size: 28rpx;
				margin: 25rpx 0;
			}

			.subBtn {
				font-size: 30rpx;
				color: white;
				padding: 8rpx 95rpx;
				background: #5D9CEB;
				width: fit-content;
				margin: 137rpx auto 0rpx;
				border-radius: 10rpx;
			}
		}
	}
</style>